import Vue from 'vue'
import VueRouter from 'vue-router'
import firstPage from '../views/firstPage.vue'
import register from '../views/register.vue'
import login from '../views/login.vue'
import home from '../views/home.vue'
import chat from '../views/chat.vue'
import mainlist from '../views/maillist.vue'
import moments from '../views/moments.vue'
import mine from '../views/mine.vue'
import friendsmoments from '../views/friendsmoment.vue'
import writemsgs from '../views/writeDemo.vue'
import userall from '../views/userall.vue'
import studymyself from '../views/tag/studymyself.vue'
import toroom from '../views/tag/toroom.vue'
import selfRoom from '../views/selfRoom.vue'
import tochar from '../views/chatmsg.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/home', redirect: '/home/chat' },
  { path: '/', component: firstPage },
  { path: '/register', component: register },
  { path: '/login', component: login },
  { path: '/friendsmoments', component: friendsmoments },
  { path: '/writemsgs', component: writemsgs },
  { path: '/userall', component: userall },
  { path: '/selfRoom', component: selfRoom },
  { path: '/tochar', component: tochar },
  {
    path: '/home', component: home, children: [
      { path: 'chat', component: chat },
      {
        path: 'maillist', component: mainlist, children: [
          { path: 'studymyself', component: studymyself },
          { path: 'toroom', component: toroom }
        ]
      },
      { path: 'Moments', component: moments },
      { path: 'mine', component: mine }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫//导航守卫
//  to代表要访问的路由地址 from表示从哪里来  next表示通行
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/register' || to.path === '/') {
    return next()
  } else {
    if (to.path.includes('/home') || to.path.includes('/friendsmoments')||to.path === '/writemsgs'||to.path === '/userall'||to.path === '/selfRoom'||to.path === '/tochar') {
      const tokenid = window.sessionStorage.getItem('token')
      if (!tokenid) {
        alert('您还未登录，请先登录~')
        next('/login')
      } else {
        next()
      }
    }
  }
})

export default router
